package com.tengroup.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tengroup.entity.WrongQuestionEntity;
import com.tengroup.service.WrongQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wrong-question")
public class WrongQuestionController {

    @Autowired
    private WrongQuestionService wrongQuestionService;

    @PostMapping
    public void add(@RequestBody WrongQuestionEntity wrongQuestionEntity) {
        wrongQuestionService.save(wrongQuestionEntity);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        wrongQuestionService.removeById(id);
    }

    @PutMapping
    public void update(@RequestBody WrongQuestionEntity wrongQuestionEntity) {
        wrongQuestionService.updateById(wrongQuestionEntity);
    }

    @GetMapping("/{id}")
    public WrongQuestionEntity getById(@PathVariable Integer id) {
        return wrongQuestionService.getById(id);
    }

    @GetMapping("/page")
    public Page<WrongQuestionEntity> listByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return wrongQuestionService.page(new Page<>(pageNum, pageSize));
    }

    @GetMapping("/generate-paper")
    public List<WrongQuestionEntity> generateWrongQuestionPaper(
            @RequestParam Integer userId,
            @RequestParam Integer courseId) {
        return wrongQuestionService.lambdaQuery()
                .eq(WrongQuestionEntity::getCourseId, courseId)
                .list();
    }

    /**
     * 根据难易程度和科目生成训练题
     *
     * @param difficulty 难易程度
     * @param courseId   科目ID
     * @return 符合条件的错题列表
     */
    @GetMapping("/generate-training-paper")
    public List<WrongQuestionEntity> generateTrainingPaper(
            @RequestParam String difficulty,
            @RequestParam Integer courseId) {
        return wrongQuestionService.generateTrainingPaper(difficulty, courseId);
    }
}